﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TARADesktopPatient
{
	/// <summary>
	/// Interaction logic for HumanBody.xaml
	/// </summary>
	public partial class HumanBody : UserControl
	{
		public HumanBody()
		{
			this.InitializeComponent();

            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
            bgWorker.WorkerSupportsCancellation = true;

            //run background worker
            bgWorker.RunWorkerAsync(500);
		}

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            #region forloopjointList
            foreach (KeyValuePair<int, int> temp in MainPage.jointList)
            {
                switch (temp.Key)
                {
                    case 1:
                        btnNeck.Background = Brushes.Red;
                        break;
                    case 2:
                        btnLeftShoulder.Background = Brushes.Red;
                        break;
                    case 3:
                        btnRightShoulder.Background = Brushes.Red;
                        break;
                    case 4:
                        btnLeftElbow.Background = Brushes.Red;
                        break;
                    case 5:
                        btnRightElbow.Background = Brushes.Red;
                        break;
                    case 6:
                        btnLeftWrist.Background = Brushes.Red;
                        break;
                    case 7:
                        btnRightWrist.Background = Brushes.Red;
                        break;
                    case 9:
                        btnLeftHip.Background = Brushes.Red;
                        break;
                    case 10:
                        btnRightHip.Background = Brushes.Red;
                        break;
                    case 11:
                        btnLeftKnee.Background = Brushes.Red;
                        break;
                    case 12:
                        btnRightKnee.Background = Brushes.Red;
                        break;
                    case 13:
                        btnLeftAnkle.Background = Brushes.Red;
                        break;
                    case 14:
                        btnRightAnkle.Background = Brushes.Red;
                        break;
                }
            }
            #endregion
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //throw new NotImplementedException();

            //Dispatcher.BeginInvoke((Action) =>
            //{

            //});
        }

        int jointID;

        private Button previousButton = null;
        private void ResetPreviousButton(Button btn)
        {
            
            if (previousButton != null)
            {
                if (previousButton != btn)
                {
                    previousButton.BorderBrush = Brushes.Black;
                }              
            }
            previousButton = btn;
        }

        #region joints click
        private void btnNeck_Click(object sender, RoutedEventArgs e)
        {
            jointID = 1;
            lblMaxAngle.Text = "Nil";
            lblMinAngle.Text = "Nil";
            displayJointSeverity();
            btnNeck.BorderBrush = Brushes.White;
            lblJoint.Text = "Neck";
            ResetPreviousButton(btnNeck);
        }

        private void btnLeftShoulder_Click(object sender, RoutedEventArgs e)
        {
            jointID = 2;
            #region show MaxMin
            string minX = "";
            string minY = "";
            string minZ = "";
            string maxX = "";
            string maxY = "";
            string maxZ = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 6)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle X":
                                maxX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Y":
                                maxY = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Z":
                                maxZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle X":
                                minX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Y":
                                minY = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Z":
                                minZ = gp.GameValue;
                                counter++;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(maxX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxY)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxZ)).ToString() + "(X, Y, Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(minX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minY)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minZ)).ToString() + "(X, Y, Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnLeftShoulder.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Shoulder";
            ResetPreviousButton(btnLeftShoulder);
        }

        private void btnRightShoulder_Click(object sender, RoutedEventArgs e)
        {
            jointID = 3;
            #region show MaxMin
            string minX = "";
            string minY = "";
            string minZ = "";
            string maxX = "";
            string maxY = "";
            string maxZ = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 6)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle X":
                                maxX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Y":
                                maxY = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Z":
                                maxZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle X":
                                minX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Y":
                                minY = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Z":
                                minZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(maxX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxY)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxZ)).ToString() + "(X, Y, Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(minX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minY)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minZ)).ToString() + "(X, Y, Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnRightShoulder.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Shoulder";
            ResetPreviousButton(btnRightShoulder);
        }

        private void btnLeftElbow_Click(object sender, RoutedEventArgs e)
        {
            jointID = 4;
            #region show MaxMin
            string min = "";
            string max = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 2)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle":
                                max = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle":
                                min = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(max)).ToString() + "(Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(min)).ToString() + "(Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnLeftElbow.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Elbow";
            ResetPreviousButton(btnLeftElbow);
        }

        private void btnRightElbow_Click(object sender, RoutedEventArgs e)
        {
            jointID = 5;
            #region show MaxMin
            string min = "";
            string max = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 2)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle":
                                max = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle":
                                min = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(max)).ToString() + "(Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(min)).ToString() + "(Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnRightElbow.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Elbow";
            ResetPreviousButton(btnRightElbow);
        }

        private void btnLeftWrist_Click(object sender, RoutedEventArgs e)
        {
            jointID = 6;
            lblMaxAngle.Text = "Nil";
            lblMinAngle.Text = "Nil";
            displayJointSeverity();
            btnLeftWrist.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Wrist";
            ResetPreviousButton(btnLeftWrist);
        }

        private void btnRightWrist_Click(object sender, RoutedEventArgs e)
        {
            jointID = 7;
            lblMaxAngle.Text = "Nil";
            lblMinAngle.Text = "Nil";
            displayJointSeverity();
            btnRightWrist.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Wrist";
            ResetPreviousButton(btnRightWrist);
        }

        private void btnLeftHip_Click(object sender, RoutedEventArgs e)
        {
            jointID = 9;
            #region show MaxMin
            string minX = "";
            string minZ = "";
            string maxX = "";
            string maxZ = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 4)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle X":
                                maxX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Z":
                                maxZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle X":
                                minX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Z":
                                minZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(maxX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxZ)).ToString() + "(X, Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(minX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minZ)).ToString() + "(X, Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnLeftHip.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Hip";
            ResetPreviousButton(btnLeftHip);
        }

        private void btnRightHip_Click(object sender, RoutedEventArgs e)
        {
            jointID = 10;
            #region show MaxMin
            string minX = "";
            string minZ = "";
            string maxX = "";
            string maxZ = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 4)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle X":
                                maxX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Max Angle Z":
                                maxZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle X":
                                minX = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle Z":
                                minZ = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(maxX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(maxZ)).ToString() + "(X, Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(minX)).ToString() + ", " + Math.Truncate(Convert.ToDouble(minZ)).ToString() + "(X, Z)";
            }
            #endregion
            displayJointSeverity();
            btnRightHip.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Hip";
            ResetPreviousButton(btnRightHip);
        }

        private void btnLeftKnee_Click(object sender, RoutedEventArgs e)
        {
            jointID = 11;
            #region show MaxMin
            string min = "";
            string max = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 2)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle":
                                max = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle":
                                min = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(max)).ToString() + "(Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(min)).ToString() + "(Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnLeftKnee.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Knee";
            ResetPreviousButton(btnLeftKnee);
        }

        private void btnRightKnee_Click(object sender, RoutedEventArgs e)
        {
            jointID = 12;
            #region show MaxMin
            string min = "";
            string max = "";
            int counter = 0;
            bool nill = false;
            foreach (tara_wcf.gamePlay gp in MainPage.gpList)
            {
                if (counter <= 2)
                {
                    if (gp.JointID == jointID)
                    {
                        switch (gp.GameKey)
                        {
                            case "Max Angle":
                                max = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                            case "Min Angle":
                                min = gp.GameValue;
                                counter++;
                                nill = true;
                                break;
                        }
                    }
                }
            }
            if (nill == true)
            {
                lblMaxAngle.Text = Math.Truncate(Convert.ToDouble(max)).ToString() + "(Z)";
                lblMinAngle.Text = Math.Truncate(Convert.ToDouble(min)).ToString() + "(Z)";
            }
            else
            {
                lblMaxAngle.Text = "Nil";
                lblMinAngle.Text = "Nil";
            }
            #endregion
            displayJointSeverity();
            btnRightKnee.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Knee";
            ResetPreviousButton(btnRightKnee);
        }

        private void btnLeftAnkle_Click(object sender, RoutedEventArgs e)
        {
            jointID = 13;
            lblMaxAngle.Text = "Nil";
            lblMinAngle.Text = "Nil";
            displayJointSeverity();
            btnLeftAnkle.BorderBrush = Brushes.White;
            lblJoint.Text = "Left Ankle";
            ResetPreviousButton(btnLeftAnkle);
        }

        private void btnRightAnkle_Click(object sender, RoutedEventArgs e)
        {
            jointID = 14;
            lblMaxAngle.Text = "Nil";
            lblMinAngle.Text = "Nil";
            displayJointSeverity();
            btnRightAnkle.BorderBrush = Brushes.White;
            lblJoint.Text = "Right Ankle";
            ResetPreviousButton(btnRightAnkle);
        }

        #endregion

        private void displayJointSeverity()
        {
            foreach (KeyValuePair<int, int> temp in MainPage.jointList)
            {
                if (temp.Key.Equals(jointID))
                {
                    lblSeverity.Text = temp.Value.ToString();
                    break;
                }
                else
                {
                    lblSeverity.Text = "0";
                }
            }
        }
    }
}